package com.rovinj_server.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.rovinj_server.R;
import com.rovinj_server.adapter.InventroyAllocationAdapter;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.Inventory;
import com.rovinj_server.entity.InventorySub;
import com.rovinj_server.entity.InventoryTask;
import com.zhy.autolayout.AutoLayoutActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/8.
 */

public class InventoryAllocationActivity extends AutoLayoutActivity {

    @Bind(R.id.location)
    TextView mLocation;
    @Bind(R.id.zong_nums)
    TextView mZongNums;
    @Bind(R.id.but_xin)
    Button mButXin;
    @Bind(R.id.butt_chong)
    Button mButtChong;
    @Bind(R.id.butt_wang)
    Button mButtWang;
    @Bind(R.id.list_view)
    ListView mListView;
    private List<InventorySub> mListInventory;
    private List<InventorySub> mListTyme = new ArrayList<>();
    private List<InventoryTask> mInventoryTask = new ArrayList<>();
    private List<InventoryTask> mTymeTask = new ArrayList<>();

    private InventroyAllocationAdapter mAdapter;
    private DBManager mDbManager;
    public  boolean wang=false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wu);
        ButterKnife.bind(this);
        mDbManager = new DBManager(InventoryAllocationActivity.this);
        mDbManager.copyDBFile();
        initData();
        initView();
        hidStater();
    }

    private void initView() {
        mInventoryTask = mDbManager.findTaskList();
        mAdapter = new InventroyAllocationAdapter(this, mInventoryTask);
        mListView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }

    private void initData() {
        int num = 0;
        Intent intent = getIntent();
        if (intent != null) {
            mListInventory = (List<InventorySub>) intent.getSerializableExtra("mListInventory");
            if (mListInventory != null && mListInventory.size() > 0) {
                for (int i = 0; i < mListInventory.size(); i++) {
                    num = num + (Integer.parseInt(mListInventory.get(i).getSjnumber()));
                }
            }
            mLocation.setText(mListInventory.size() + "");
            mZongNums.setText(num + "");
        }
    }

    /**
     * 隐藏状态栏
     */
    private void hidStater() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    @OnClick({R.id.but_xin, R.id.butt_chong, R.id.butt_wang, R.id.fh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.but_xin:
                popup();
                break;
            case R.id.fh:
                finish();
                break;
            case R.id.butt_chong:
                int i = mDbManager.dedeteData(2);
                mInventoryTask.clear();
                mAdapter.setListDataBean(mInventoryTask);
                mAdapter.notifyDataSetChanged();
                Toast.makeText(this, "清空" + i, Toast.LENGTH_SHORT).show();
                break;
            case R.id.butt_wang:
                if(wang && mTymeTask!=null &&mTymeTask.size()>0){
                    int j = mDbManager.insertInventoryData(mTymeTask);
                    mTymeTask.clear();
                    if(j==1){
                        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "请新建任务", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    public void select_cancel(View view) {
        popupWindow.dismiss();
    }

    private PopupWindow popupWindow;
    private EditText mEditName;
    private EditText mEditXiao;
    private EditText mEditDa;
    private Button mButt;

    private void popup() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.demand_popup_select, null);
        popupWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.popup_window);
        popupWindow.setContentView(view);
        mEditName = (EditText) view.findViewById(R.id.editText_name);
        mEditXiao = (EditText) view.findViewById(R.id.editText_xiao);
        mEditDa = (EditText) view.findViewById(R.id.editText_da);
        mButt = (Button) view.findViewById(R.id.button);
        mButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListTyme.clear();
                int sjQty = 0;
                int diffQty = 0;
                String name = mEditName.getText().toString();
                String xiao = mEditXiao.getText().toString();
                String da = mEditDa.getText().toString();
                String location = mLocation.getText().toString();
                if (!name.equals("") && !xiao.equals("") && !da.equals("")) {
                    int x = Integer.parseInt(xiao);
                    int d = Integer.parseInt(da);
                    int l = Integer.parseInt(location);
                    if (d >= x && d <= l) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String t = format.format(new Date());
                        int numcode = (int) ((Math.random() * 9 + 1) * 1000);
                        InventoryTask inventoryTask = new InventoryTask();
                        inventoryTask.setCode(name + "---" + numcode);
                        inventoryTask.setCrDate(t + "");
                        inventoryTask.setName(mEditName.getText().toString());
                        inventoryTask.setRange(xiao + "--" + d);
                        for (int i = 0; i < mListInventory.size(); i++) {
                            if (i >= x && i <= d) {
                                int sj = Integer.parseInt(mListInventory.get(i).getSjnumber().toString());
                                sjQty = sjQty + sj;
                                diffQty = diffQty + Integer.parseInt(mListInventory.get(i).getDiffNumber().toString());
                                mListTyme.add(mListInventory.get(i));
                                inventoryTask.setInventorySubList(mListTyme);
                            }
                        }
                        wang=true;
                        inventoryTask.setSpQty(sjQty);
                        inventoryTask.setDiffQty(diffQty);
                        mInventoryTask.add(inventoryTask);
                        mTymeTask.add(inventoryTask);
                        mAdapter.setListDataBean(mInventoryTask);
                        mAdapter.notifyDataSetChanged();
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InventoryAllocationActivity.this, "货位数量过多", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(InventoryAllocationActivity.this, "请填姓名或者数量填写正常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });

        // popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
    /*    popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);*/
        WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
        params.alpha = 0.8f;
        getWindow().setAttributes(params);//把该参数对象设置进当前界面中
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                getWindow().setAttributes(params);
            }
        });

        //第一个参数为父View对象，即PopupWindow所在的父控件对象，第二个参数为它的重心，后面两个分别为x轴和y轴的偏移量
        popupWindow.showAtLocation(inflater.inflate(R.layout.activity_main, null), Gravity.CENTER, 0, 0);
        //popupWindow.showAsDropDown(view,100,100);
    }
}
